/*
 * Главное меню программы
 */

package mops.menu;

import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuBarBuilder;
import javafx.scene.control.MenuBuilder;
import javafx.scene.control.MenuItemBuilder;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCombination;
import javafx.stage.Stage;
import mops.commons.Commons;
import mops.menu.items.ItemsAction;

/**
 *
 * @author Жук Александр
 */
public class GeneralMenu{
    
    private final MenuBar mainMenu;
    private final Stage fStage;
    
    public  GeneralMenu(final Stage currentStage){ 
      fStage = currentStage;  
      mainMenu = createMenus();
    }
       
    //Создание главного меню
    private MenuBar createMenus() {
      MenuBar menuBar = MenuBarBuilder.create()
        .menus(
            //МОПС    
            MenuBuilder.create()
            .text(Commons.MENU_APPLICATION)
            .items(
                    MenuItemBuilder.create()
                    .text(Commons.MENU_APPLICATION_OPTIONS)
                    .graphic((new ImageView(
                        new Image(getClass().getResourceAsStream(Commons.MENU_APPLICATION_OPTIONS_PICTURE))))
                    )
                    .onAction(ItemsAction.OnOptionsAction(fStage))
                    .build(),
                    new SeparatorMenuItem(),
                    MenuItemBuilder.create()
                    .text(Commons.MENU_APPLICATION_EXIT)
                    .onAction(ItemsAction.OnExitAction(fStage))
                    .build()
                )
            .build(),
            //Управление данными
             MenuBuilder.create()
            .text(Commons.MENU_DATACONTROL)
            .items(
                    MenuBuilder.create()
                    .text(Commons.MENU_DATACONTROL_LIBRARY)
                    .graphic((new ImageView(
                      new Image(getClass().getResourceAsStream(Commons.MENU_DATACONTROL_LIBRARY_PICTURE))))
                     )             
                    .items(
                            MenuBuilder.create()
                            .text(Commons.MENU_DATACONTROL_LIBRARY_FLAG)
                            .items(
                                    MenuItemBuilder.create()
                                       .text(Commons.MENU_DATACONTROL_LIBRARY_MMSI)
                                       .onAction(ItemsAction.OnMMSIAction(fStage)) 
                                        .graphic((new ImageView(
                                         new Image(getClass().getResourceAsStream(Commons.MENU_DATACONTROL_LIBRARY_MMSI_PICTURE))))
                                        )     
                                       .build(),
                                    MenuItemBuilder.create()
                                       .text(Commons.MENU_DATACONTROL_LIBRARY_COUNTRY)
                                       .onAction(ItemsAction.OnCountryAction(fStage))
                                       .graphic((new ImageView(
                                         new Image(getClass().getResourceAsStream(Commons.MENU_DATACONTROL_LIBRARY_COUNTRY_PICTURE))))
                                        )     
                                       .build(),
                                    MenuItemBuilder.create()
                                       .text(Commons.MENU_DATACONTROL_LIBRARY_OPERATOR)
                                       .onAction(ItemsAction.OnOperAction(fStage))
                                       .graphic((new ImageView(
                                         new Image(getClass().getResourceAsStream(Commons.MENU_DATACONTROL_LIBRARY_OPERATOR_PICTURE))))
                                        )    
                                       .build()
                                  )
                            .build()
                           )
                    .build()             
                    )
            .build(),    
            
            //Справка
            MenuBuilder.create()
            .text(Commons.MENU_HELP)
            .items(
                    MenuItemBuilder.create()
                    .text(Commons.MENU_HELP_GET)
                    .accelerator(KeyCombination.keyCombination(Commons.MENU_HELP_GET_HOTKEY))
                    .onAction(ItemsAction.OnHelpAction(fStage))
                    .graphic((new ImageView(
                        new Image(getClass().getResourceAsStream(Commons.MENU_HELP_GET_PICTURE))))
                    )
                    .build(),
                    new SeparatorMenuItem(),
                    MenuItemBuilder.create()
                    .text(Commons.MENU_HELP_ABOUT)
                    .onAction(ItemsAction.OnAboutAction(fStage))
                    .build()
                    )
            .build()
            )
            .build();
      
        return menuBar;
    }

    //Получение главного меню    
    public MenuBar getMainMenu() throws Exception {
        if (mainMenu!=null){
          return mainMenu;  
        }
        else
        {
          throw new Exception(Commons.GET_MENU_ERROR);  
        }                
    }

}
